package com.iot08.api.controller;

import com.iot08.api.service.*;
import com.iot08.api.service.supplement.CommonService;
import com.iot08.api.vo.request.*;
import com.iot08.api.vo.request.base.BaseAccountReqVO;
import com.iot08.common.utils.response.ResponseMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0
 * @Description:
 * @time: 2020/12/18 14:19
 * @author: Rock
 */
@RestController
@RequestMapping("getway")
public class CommonController {

    @Resource
    private DeviceService deviceService;
    @Resource
    private CommonService commonService;
    @Resource
    private KnowledgeService knowledgeService;
    @Resource
    private StatisticsListService statisticsListService;
    @Resource
    private AdvertService advertService;
    @Resource
    private VersionupdatesService versionupdatesService;
    @Resource
    private FeedbackService feedbackService;
    @Resource
    private DeviceNotificationService deviceNotificationService;
    @Resource
    private CourseService courseService;

    /**
     * 是否在免打扰的时间内检查
     *
     * @param imei
     * @param vo
     * @param bindingResult
     * @time: 2020/12/18 14:41
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/disturbbancheck/{imei}")
    public ResponseMap disturbBanCheck(@PathVariable("imei") String imei,
                                       @Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return deviceService.disturbBanCheck(imei, vo);
    }

    /**
     * 通知设备付款成功的消息
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/12/25 17:04
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("/notice_payment_with_dev")
    public ResponseMap noticePayMentWithDev(@Validated NoticePayMentWithDevReqVO vo, BindingResult bindingResult) {
        return commonService.noticePayMentWithDev(vo);
    }

    /**
     * 知识栏目配置接口
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/12/25 17:12
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/knowledgeconfig")
    public ResponseMap getKnowledgeConfig(@Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return knowledgeService.getInfo();
    }

    /**
     * 统计
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/12/26 9:20
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("/statistics")
    public ResponseMap statistics(@Validated StatisticsReqVO vo, BindingResult bindingResult) {
        return statisticsListService.statistics(vo);
    }

    /**
     * 获取自己的广告配置 (媒体位)
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/1/4 11:06
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("self_ad_config")
    public ResponseMap getSelfAdvertConfig(@Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return advertService.getInfo(vo);
    }

    /**
     * 版本升级查询
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/1/5 11:03
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("version")
    public ResponseMap version(@Validated VersionReqVO vo, BindingResult bindingResult) {
        return versionupdatesService.version(vo);
    }

    /**
     * 应用更新
     *
     * @param packageName
     * @param packageVersion
     * @param channel
     * @time: 2021/1/5 16:06
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/update/android")
    public ResponseMap getUpdateAndroid(@RequestParam(value = "package", required = false) String packageName,
                                        @RequestParam(required = false) String packageVersion,
                                        @RequestParam(required = false) String channel) {
        return versionupdatesService.getUpdateAndroid(packageName, packageVersion, channel);
    }

    /**
     * 反馈问题 (客户反馈/意见反馈)
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/1/5 16:47
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("feedback")
    public ResponseMap feedback(@Validated FeedbackReqVO vo, BindingResult bindingResult) {
        return feedbackService.add(vo);
    }

    /**
     * 上传广告资源信息接口
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/1/6 9:40
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("adupdate")
    public ResponseMap adupdate(@Validated AdupdateReqVO vo, BindingResult bindingResult) {
        return deviceNotificationService.adupdate(vo);
    }

    /**
     * 应用监管 设备商店应用权限列表获取
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/1/9 15:54
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("monitoring-watch-shop")
    public ResponseMap getMonitoringWatchShop(@Validated GetMonitoringWatchShopReqVO vo, BindingResult bindingResult) {
        return commonService.getMonitoringWatchShop(vo);
    }

    /**
     * APP修改 设备商店应用权限列表调用的接口, 调用完 会下发给设备,触发设备自定义事件
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/11/23 17:06
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("monitoring-watch-shop")
    public ResponseMap monitoringWatchShop(@Validated MonitoringWatchShopReqVO vo, BindingResult bindingResult) {
        return commonService.monitoringWatchShop(vo);
    }

//    /**
//     * 日志的获取是否已上传的信息
//     *
//     * @param uuid
//     * @time: 2021/1/28 10:59
//     * @author: Rock
//     * @version 1.0
//     */
//    @GetMapping("logupdate_interface")
//    public ResponseMap getLogupdateInterface(@RequestParam String uuid) {
//        return commonService.getLogupdateInterface(uuid);
//    }

    /**
     * 日志的上传
     *
     * @param openid
     * @param logzipFile
     * @time: 2021/1/28 10:59
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("logupdate_interface")
    public ResponseMap logupdateInterface(@RequestHeader(value = "User-Agent", required = false) String userAgent,
                                          @RequestParam String openid, MultipartFile logzipFile) throws IOException {
        return commonService.logupdateInterface(openid, userAgent, logzipFile);
    }

//    /**
//     * 设置提取 特定UUID 设备的日志
//     *
//     * @param uuid
//     * @time: 2021/1/28 11:00
//     * @author: Rock
//     * @version 1.0
//     */
//    @PostMapping("setuuidwithgetlog")
//    public ResponseMap setuuidwithgetlog(@RequestParam String uuid) {
//        return commonService.setuuidwithgetlog(uuid);
//    }

    /**
     * 小程序配置接口 (2.0V以后废弃)
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/2/26 11:49
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("small_program_config")
    public ResponseMap getSmallProgramConfig(@Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return commonService.getSmallProgramConfig(vo);
    }

    /**
     * 轮播图 (2.0V以后废弃)
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/2/26 11:54
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("slideshow")
    public ResponseMap getSlideshow(@Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return commonService.getSlideshow(vo);
    }

    /**
     * 统计轮播图点击数 (2.0V以后废弃)
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/2/26 11:54
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("slideshow")
    public ResponseMap postSlideshow(@Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return ResponseMap.success();
    }

    /**
     * 统一 下载文件
     *
     * @param filename
     * @time: 2021/3/26 15:15
     * @author: Rock
     * @version 1.0
     */
//    @GetMapping("download/{path:.+}")
//    public void download(@PathVariable("path") String path, HttpServletResponse response) throws IOException {
//        commonService.download(path, response);
//    }

    @GetMapping("download/**")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] split = request.getRequestURI().split("/download/");
        String path = split[1];
        commonService.download(path, response);
    }


//    /**
//     * 下载miniIO文件
//     */
//    @GetMapping("miniIO/{filename:.+}")
//    public void miniIO(@PathVariable("filename") String filename, HttpServletResponse response) throws IOException {
//        commonService.miniIO(filename, response);
//    }

    /**
     * @time: 2021/5/31 14:59
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("course/list")
    public ResponseMap listCourse() {
        return courseService.list();
    }


}
